<template>
  <div id="app">
    <router-view></router-view>
  </div>
</template>

<script>
import Cookies from "js-cookie";
import httpRequest from "@/api/httpRequest";
import { getUserInfo } from "@/api/my";

export default {
  name: "App",
  components: {},
  data() {
    return {
      activeChat: {
        targetId: 0,
        messages: [],
        type: "PRIVATE",
      },
      chat: {},
      current: 0,
      size: 100,
    };
  },
  async mounted() {
    await this.$store.dispatch("getUserInfo");
    // console.log('isWsApi',sessionStorage.getItem("isWsApi"));
    // if (sessionStorage.getItem("isWsApi") == 1) {
    //   console.log('已经登录子系统并创建websocket链接，return')
    //   return;
    // }
    // if (this.$store.state.SET_FlAG == 1) {
    // 调用 请求用户信息的接口 ---登录子系统--拿到子系统的token------
    await this.$store.dispatch("loginChat", this.$store.state.userInfo); //拿到主系统的userInfo去登录聊天系统 并登录成功
    await this.getChatList().then((res) => {
      console.log("请求列表，数据变换");
      this.$store.commit("setYaseeList", res.records);
    });
    httpRequest({
      url: "/user/self",
      methods: "get",
    }).then((data) => {
      //查询子系统 用户信息
      // this.$store.commit('SET_CHILDREN_USER_INFO', data)
      this.init(data);
    });
  },
  methods: {
    getChatList() {
      let query = {
        current: this.current,
        size: 100,
      };
      return new Promise((resolve, reject) => {
        httpRequest({
          url: `/friend/cmsFriends`,
          method: "get",
          params: query,
        }).then((res) => {
          resolve(res);
        });
      });
    },
    async init(userInfo) {
      console.log("子系统的用户信息consulation_list", userInfo);
      this.$store.commit("setUserInfo", userInfo); //把子系统的用户信息 存store
      this.$store.commit("setUserState", this.$enums.USER_STATE.FREE); //用户是否在线 存store
      // if (this.$route.meta.title == '客服咨询') {
      //v.0.1.1新需求 添加官方客服 商家客服信息 粉丝 在线问诊
      // this.$store.commit("initStore"); //好友列表
      // } else {
      //创建一个官方客服
      // let kfList = await this.getKfList()
      // console.log('kfList', kfList)
      // this.activeChat.headImage = kfList.headImage;
      // this.activeChat.headImageThumb = kfList.headImageThumb
      // this.activeChat.showName = kfList.nickName
      // this.activeChat.targetId = kfList.id
      // this.chat = this.activeChat
      // const recordContent = await this.getRecondList();
      // this.activeChat.messages = recordContent
      // this.$store.commit('editChats', this.activeChat)
      // }
      // this.$forceUpdate()
      // this.$wsApi.createWebSocket('wss://wechat.yasee.com.cn:8878/im', userInfo.id); //初始化websocket链接
      // this.$wsApi.createWebSocket('ws://192.168.90.225:8878/im', userInfo.id); //初始化websocket链接
      this.$wsApi.createWebSocket(
        `${this.$store.state.websocket}`,
        userInfo.id
      ); //初始化websocket链接
      // sessionStorage.setItem("isWsApi", 1);
      // this.$store.commit('SET_FlAG',1)
      this.$wsApi.onopen(() => {
        // this.pullUnreadMessage();
      });
      this.$wsApi.onmessage((cmd, msgInfo) => {
        console.log("cmd", cmd);
        console.log("msgInfo", msgInfo);
        if (cmd == 2) {
          // 异地登录，强制下线
          this.$store.commit("setChildrenFlag", 0);
          // this.$message.error("您已在其他地方登陆，将被强制下线");
          // setTimeout(() => {
          //   location.href = "/";
          // }, 1000)
          this.$wsApi.closeWebSocket();
        } else if (cmd == 3) {
          // 插入私聊消息
          // this.handlePrivateMessage(msgInfo);
          if (msgInfo.userType == 5 || msgInfo.userType == 6) {
            //商家客服新消息处理
            this.$store.commit("insertShopMessage", msgInfo);
          } else {
            //官方客服和好友列表发来的消息提醒
            this.$store.commit("insertMessage", msgInfo);
          }
          // this.$forceUpdate()
          // 播放提示音
          // this.playAudioTip();
        }
        // else if (cmd == 4) {
        //   // 插入群聊消息
        //   this.handleGroupMessage(msgInfo);
        // }
      });
    },
    handlePrivateMessage(msg) {
      // 好友列表存在好友信息，直接插入私聊消息
      let friend = this.$store.state.friendStore.friends.find(
        (f) => f.id == msg.sendId
      );
      // let friend = chatStore.state.chats.find((f) => f.id == msg.sendId)
      if (friend) {
        this.insertPrivateMessage(friend, msg);
        // return;
      }
      // 好友列表不存在好友信息，则发请求获取好友信息
      // httpRequest({
      //   url: `/friend/find/${msg.sendId}`,
      //   method: 'get'
      // }).then((friend) => {
      //   this.insertPrivateMessage(friend, msg);
      //   this.$store.commit("addFriend", friend);
      // })
    },
    insertPrivateMessage(friend, msg) {
      console.log("处理私人消息");
      // let chatInfo = {
      //   type: 'PRIVATE',
      //   targetId: friend.id,
      //   showName: friend.nickName,
      //   headImage: friend.headImage
      // };
      // 打开会话
      // this.$store.commit("openChat", chatInfo);
      // 插入消息
      this.$store.commit("insertMessage", msg);
      this.$forceUpdate();
      // 播放提示音
      // this.playAudioTip();
    },
  },
};
</script>

<style lang="less" scoped>
#app {
  font-family: siyuan;
  font-style: normal;
  background-color: #ffffff;
  min-height: 100%;
}
</style>
